﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class ScoreText : MonoBehaviour {
    int myTag;
    int score;

    public float timer;
    float needTime;
    public bool stopFlag;
    public bool startFlag;

    Vector3 src;
    Vector3 dst;
    RectTransform rectTransform;
    Text text;

    private void Awake()
    {
        timer = .0f;
        stopFlag = false;
        startFlag = false;

        rectTransform = gameObject.GetComponent<RectTransform>();
        text = gameObject.GetComponent<Text>();        
    }

    // Update is called once per frame
    void Update () {
        if (startFlag)
        {
            if (stopFlag)
            {
                return;
            }
            TextMove(myTag);
        }
    }

    public void Init(int tag, int score = 0, int multiple = 0)
    {
        myTag = tag;
        this.score = score;
        src = rectTransform.anchoredPosition3D;

        switch (tag) {
            case 0: // score
                dst = src - new Vector3(0, 30.0f);
                needTime = 0.2f;
                break;
            case 1: // scoreText
                dst = src + new Vector3(0, 10.0f);
                needTime = 1.0f;
                text.text = "+" + score;
                break;
            case 2: // multipleText
                dst = src;
                needTime = 1.0f;
                text.text = "翻倍\nX " + multiple;
                break;
            default:
                Debug.Log("Init() error!");
                break;
        }
        startFlag = true;
    }

    void TextMove(int tag)
    {
        timer += Time.deltaTime;
        if (timer >= needTime)
        {
            timer = needTime;
            stopFlag = true;
        }
        rectTransform.anchoredPosition3D = src + (dst - src) * (timer / needTime);
        src = rectTransform.anchoredPosition3D;
        Color oldColor = text.color;
        switch (tag)
        {
            // Score
            case 0:
                
                break;
            // ScoreText
            case 1:
                text.color = new Color(oldColor.r, oldColor.g, oldColor.b, (1 - timer) / needTime);
                if (stopFlag)
                {
                    Destroy(gameObject);
                }
                break;
            case 2:
                text.color = new Color(oldColor.r, oldColor.g, oldColor.b, (1 - timer) / needTime);
                break;
        }
    }
}
